<?php

namespace admin\helper;

use admin\helper\common;

class combo extends common
{

    public function findCombo($product_id)
    {
        $map['product_id'] = $product_id;
        $map['is_del'] = 0;
        $ret = $this->db->select('combo', "*", $map);
        $combo = [];
        if ($ret) {
            $combo_id = array_column($ret, 'combo_id');
            $comboGoods = $this->getComboGoods($combo_id);

            foreach ($ret as $value) {
                $value['price'] = money_int($value['price'], 'float');
                foreach ($comboGoods as $goods) {
                    if ($goods['combo_id'] == $value['combo_id']) {
                        $value['goodsList'][] = $goods;
                    }

                }
                $combo[] = $value;
            }
        }
        return $combo;

    }

    public function getComboGoods($combo_id)
    {

        $map['combo_id'] = $combo_id;
        $ret = $this->db->select('combo_goods', "*", $map);

        return $ret;
    }

    /**
     * 删除套餐、套餐产品列表
     * @param $combo_id
     * @return array
     */
    public function delCombo($combo_id)
    {
        //查询订单中是否存在该套餐
        //存在is_del=1 不存在才删除
        $map['combo_id'] = $combo_id;

        $ret = $this->db->select('order', ['order_id'], $map);
        if (!$ret) {
            $this->db->delete('combo', $map);
            $this->db->delete('combo_goods', $map);
        } else {
            $data['is_del'] = 1;
            $this->db->update('combo', $data, $map);
            $this->db->update('combo_goods', $data, $map);
        }
        return ['ret' => 1, "msg" => 'OK'];
    }

    public function  delComboGoods($id){
        $map['combo_goods_id'] = $id;
        $this->db->delete('combo_goods', $map);
        return ['ret' => 1, "msg" => 'OK'];
    }
}